//
//  DMOrginalView.swift
//  DM_新浪微博
//
//  Created by Damon.JiAo on 16/3/1.
//  Copyright © 2016年 Demon_JiAo. All rights reserved.
//

import UIKit
import SnapKit

/// 间隔
let CellMargin = 5


class DMOrginalView: UIView {
    
    var bottomConstraint:Constraint?
    
    
    var statusViewModel:DMHomeViewCellModel?{
        
        didSet{
            
            let url = NSURL(string: (statusViewModel?.cellModel?.user?.profile_image_url) ?? "")
            /** 头像设置图片 */
            photoImageView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            /** 姓名设置值 */
            nameLabel.text = statusViewModel?.cellModel?.user?.screen_name
            /** 状态文字 */
            contentLabel.text = statusViewModel?.cellModel?.text
            /** VIP */
            veriftyImageView.image = statusViewModel?.verifity_image
            /** 等级 */
            levelImageView.image = statusViewModel?.level_image
            
            /** 微博来源 */     ///     利用截取的方式进行处理
            sourceLabel.text = statusViewModel?.dealSource(statusViewModel?.cellModel?.source)
            
            /** 显示时间 */
            timeLabel.text = statusViewModel?.dealTime(statusViewModel?.cellModel?.created_at)
            
            /** 配图 */
            self.bottomConstraint?.uninstall()
            
            if let pic_urls = statusViewModel?.cellModel?.pic_urls where pic_urls.count > 0{
                
                pictureView.hidden = false
                /** 在有配图的情况下 更新约束 */
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
//                pictureView.pictureViewModel = statusViewModel
                
                var result = [DMPictureViewModel]()
                
                for model in pic_urls{
                    
                    let viewModel = DMPictureViewModel()
                    
                    viewModel.model = model
                    
                    result.append(viewModel)
                    
                }
                
                pictureView.pictureList = result
            } else {
                
                pictureView.hidden = true
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }

        }
        
    }
    
    /** 1. 三部曲 */
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /** 2. 加载各项子控件和约束 */
    private func setUpUI(){
        
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        /** 增加配图 */
        addSubview(pictureView)
        
        /** 头像约束 */
        photoImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(self.snp_top).offset(CellMargin)

            make.size.equalTo(CGSizeMake(50, 50))
        }
        
        /** 认证图片约束 */
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        
        /** 名字 */
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        
        /** 会员等级 */
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        
        /** 时间约束 */
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(CellMargin/2)
        }
        
        /** 发表来源 */
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
            
        }
        
        // 设置内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(self.snp_left).offset(CellMargin)
            
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
//                        make.right.equalTo(self.snp_right).offset(10)
            //给contentLabel固定了宽度
            make.width.equalTo(KUIScreenWidth - 2*CGFloat(CellMargin))
            //
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(CellMargin).constraint
        }
 
    }
    
    /** 3. 各项的懒加载！ */
    ///     头像
    private lazy var photoImageView:UIImageView = {
        
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        img.layer.cornerRadius = 25
        img.layer.masksToBounds = true
        
        return img
    }()
    ///     认证图片
    private lazy var veriftyImageView:UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_vip")
        
        return img
    }()
    ///     用户姓名
    private lazy var nameLabel:UILabel = {
    
        let label = UILabel()
        
        label.text = "微博姓名"
        
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    ///     会员等级
    private lazy var levelImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
    }()
    ///      发表时间
    private lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(13)
        
        label.textColor = UIColor.orangeColor()
        
        return label
        
    }()
    ///      发表来源
    private lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博来源"
        
        label.font = UIFont.systemFontOfSize(13)
        
        
        return label
        
    }()
    ///      发表内容
    private lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        label.text = "微博内容"
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.numberOfLines = 0
        
        return label
        
    }()
    ///      原创视图
    private lazy var pictureView:DMPictureView = DMPictureView()
    
}
